package com.yuxl.common.auth.service;

import com.yuxl.common.auth.entity.AuthRole;
import com.yuxl.common.auth.entity.AuthRole;
import com.yuxl.common.auth.repository.AuthRoleRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

/**
 * @author yuxl
 */
@Slf4j
@Service
public class AuthRoleService {
    @Resource
    private AuthRoleRepository authRoleRepository;

    public Flux<AuthRole> findAll() {
        return authRoleRepository.findAll();
    }

    public Mono<AuthRole> findByMark(String mark) {
        return authRoleRepository.findById(mark)
                .defaultIfEmpty(new AuthRole());
    }

    public Mono<Void> deleteByMark(String mark) {
        return authRoleRepository.deleteById(mark);
    }

    public Mono<AuthRole> create(AuthRole user) {
        user.setCreateTime(LocalDateTime.now())
                .setUpdateTime(LocalDateTime.now());
        return authRoleRepository.save(user);
    }

    public Mono<Integer> count(AuthRole user) {
        return authRoleRepository.count(user);
    }


    public Flux<AuthRole> page(AuthRole user, int page, int size) {
        long offset = (long) (page - 1) * size;
        return authRoleRepository.page(user, size, offset);
    }


    public Flux<AuthRole> list(AuthRole user) {
        return authRoleRepository.list(user);
    }

    public Mono<AuthRole> updateByMark(AuthRole user) {
        return authRoleRepository.updateById(user);
    }
} 